home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 1173 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.9 KB

  1. Path: ornews.intel.com!news
  2. From: thurman_b_miller@ccm2.hf.intel.com (Thurman Miller)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Syntax for looping through enumerated types
  5. Date: Tue, 09 Jan 1996 17:59:02 GMT
  6. Organization: Intel Corporation
  7. Message-ID: <4cuafk$rgj@ornews.intel.com>
  8. References: <4cs70o$8ns@ornews.intel.com> <4ctiuo$qsi@klein.delphi.com>
  9. NNTP-Posting-Host: thurman-pc.ssd.intel.com
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. Martin Miller <mrmiller@mci.newscorp.com> wrote:
  13.  
  14.  
  15. >Here's some things you may be able to do in specific cases, _although it doesn't 
  16. >work for enums in general_ (and pardon me if you already know most of this):
  17.  
  18. >You can take advantage of the fact that enums may be cast implicitly to int and and 
  19. >they can be explicitly cast to enum types.  This along with the important point that 
  20. >named enum values start off at zero by default and, unless overridden, increase by 
  21. >one with each successive name listed.
  22.  
  23. >In your example it means that MONDAY cast to an integer would yield 0 and that 
  24. >FRIDAY would give a value of 5.  You can also specify names that repeat integer 
  25. >values already used.  Your example could be modified as follows:
  26.  
  27. >enum daysoftheweek {MONDAY, FIRSTDAY=MONDAY/* added */, TUESDAY, WEDNESDAY, 
  28. >                    THURSDAY, FRIDAY, LASTDAY=FRIDAY/* added */};
  29.  
  30. >for (int i=FIRSTDAY; i<=LASTDAY; i++)
  31. >{
  32. >    daysoftheweek nIndex = daysoftheweek(i);
  33. >    switch (nIndex)
  34. >    {
  35. >        case  MONDAY:
  36. >            doMonday();
  37. >    }
  38. >}
  39.  
  40. Awesome! Just what the doctor ordered!
  41.  
  42. >Once again, let me reiterate that this won't work in general, because enum integer 
  43. >values can start at any where, jump around, and even repeat--but it might work for 
  44. >you if you can just setup and follow a few conventions in your own code.
  45.  
  46. Yes, but as long as we keep the def's for FIRSTDAY and LASTDAY in the
  47. eumerated list, it will work!
  48.  
  49. >As I mentioned in another post to this group--in the past in order to handle more 
  50. >general cases and reuse code, I created an extensible "class Enum" along with some 
  51. >preprocessor macros.  In the Enum class, among other things, I had member functions 
  52. >which returned the starting and ending values.  
  53.  
  54. Didn't you mention in the other post that you no longer had the source
  55. :(, because if you did, I'd love to see it!
  56.  
  57. >In addition, there were number of constructors and other operators which facilated 
  58. >not only in using them in for-statements, but also in being able to convert them to 
  59. >and from strings (ie associating MONDAY with the string "MONDAY") and 
  60. >incrementing/decrementing their values.
  61.  
  62. >In the other post I also mentioned that I wished the language itself supported some 
  63. >of these things more automatically since they seem to be a very common things to 
  64. >need and want to do.
  65.  
  66. Ditto
  67.  
  68. >If you (or anyone else reading this) have any further questions or comments about 
  69. >this subject, feel free to contact me via my email address.
  70.  
  71. >Hope this gives you some ideas.
  72.  
  73. >-- 
  74. >Martin----/\/\./\/\.
  75.  
  76.  
  77.  
  78.  
  79.